# -*- coding: utf-8 -*-
from pptx import Presentation
from pptx.util import Pt, Inches
from pptx.dml.color import RGBColor
import os
from pptx.enum.text import PP_ALIGN
import random
from PIL import Image

THEME_COLOR = RGBColor(44, 62, 80)
FONT_NAME = '微软雅黑'

# 网络图片备用
IMG_LIST = [
    "https://images.unsplash.com/photo-1506744038136-46273834b3fb",
    "https://images.unsplash.com/photo-1519125323398-675f0ddb6308",
    "https://images.unsplash.com/photo-1464983953574-0892a716854b",
    "https://images.unsplash.com/photo-1508610048659-a06b669e4c47"
]

def add_img_with_caption_random(slide, img_path, caption, page_idx):
    """
    随机选择图片排版方式：上图下文、左文右图、右图左文
    如果图片不存在或不是有效图片，插入占位框
    """
    layout_type = random.choice(['top', 'left', 'right'])
    img_width = Inches(4.5)
    img_height = Inches(2.8)
    left_margin = Inches(0.5)
    top_margin = Inches(0.5)
    text_box_width = Inches(4.5)
    text_box_height = Inches(2.8)
    if layout_type == 'top':
        img_left = Inches(1.5)
        img_top = top_margin
        text_left = left_margin
        text_top = img_top + img_height + Inches(0.2)
    elif layout_type == 'left':
        img_left = left_margin
        img_top = Inches(1.2)
        text_left = img_left + img_width + Inches(0.3)
        text_top = img_top
    else:
        img_left = Inches(5.2)
        img_top = Inches(1.2)
        text_left = left_margin
        text_top = img_top
    # 检查图片是否存在且有效
    valid_img = False
    if img_path and os.path.exists(img_path):
        try:
            with Image.open(img_path) as im:
                im.verify()
            valid_img = True
        except Exception:
            valid_img = False
    if valid_img:
        slide.shapes.add_picture(img_path, img_left, img_top, img_width, img_height)
    else:
        shape = slide.shapes.add_shape(1, img_left, img_top, img_width, img_height)
        shape.text = f"请插入第{page_idx}页图片"
        shape.text_frame.paragraphs[0].font.size = Pt(18)
        shape.text_frame.paragraphs[0].font.bold = True
        shape.text_frame.paragraphs[0].font.color.rgb = RGBColor(200, 0, 0)
    tx = slide.shapes.add_textbox(img_left, img_top + img_height, img_width, Inches(0.4))
    tf = tx.text_frame
    tf.text = caption
    tf.paragraphs[0].font.size = Pt(10)
    tf.paragraphs[0].font.italic = True
    tf.paragraphs[0].font.color.rgb = RGBColor(100, 100, 100)
    return layout_type, text_left, text_top, text_box_width, text_box_height

prs = Presentation()
prs.slide_width = Inches(13.33)
prs.slide_height = Inches(7.5)

# 1. 封面
slide = prs.slides.add_slide(prs.slide_layouts[6])
title_box = slide.shapes.add_textbox(Inches(1), Inches(2), Inches(11), Inches(1.5))
tf = title_box.text_frame
tf.text = "人工智能在现代与未来游戏开发中的应用"
tf.paragraphs[0].font.size = Pt(44)
tf.paragraphs[0].font.bold = True
tf.paragraphs[0].font.name = FONT_NAME
subtitle = slide.shapes.add_textbox(Inches(1), Inches(4), Inches(11), Inches(1))
subtitle.text = "北京理工大学继续教育学院软件工程班 2025级\n学生：耿浩 学号：202520037310009"
subtitle.text_frame.paragraphs[0].font.size = Pt(28)
subtitle.text_frame.paragraphs[0].font.name = FONT_NAME
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/1.png', "AI与游戏融合的未来图景（数据来源：Dimension Market Research 2025）", 1)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：标题、图片淡入"

# 2. 目录
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "目录"
slide.placeholders[1].text = (
    "1. 封面\n2. 目录\n3. 摘要与关键词\n4. 引言\n5. AI技术基础\n6. AI在现代游戏开发中的应用\n7. 反外挂与安全\n8. 行业数据与趋势\n9. 未来展望与挑战\n10. 个人分析与见解\n11. 结论\n12. 参考文献/致谢"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/2.png', "科技感目录配图（数据来源：Convai Blog 2024）", 2)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：目录分条淡入"

# 3. 摘要与关键词
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "摘要与关键词"
slide.placeholders[1].text = (
    "• 本文系统梳理了AI在现代与未来游戏开发中的应用、行业数据、发展趋势与挑战，结合市场报告和学术文献，提出个人分析与展望。\n\n• 关键词：人工智能；游戏开发；智能NPC；自然语言处理；计算机视觉；推荐系统；反外挂；行业趋势"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/3.png', "AI游戏市场规模增长图（数据来源：Dimension Market Research 2025）", 3)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：摘要、关键词依次淡入"

# 4. 引言
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "一、引言"
slide.placeholders[1].text = (
    "• AI推动游戏产业创新和变革\n• 丰富玩家交互体验，提升智能化水平\n• 市场规模持续扩大，AI渗透率逐年提升\n• 本文结构：技术基础、应用、数据趋势、挑战与展望"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/4.png', "全球游戏行业收入/AI渗透率（数据来源：PatentPC 2024）", 4)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：分点依次淡入，图片淡入"

# 5. AI技术基础
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "二、AI技术基础"
slide.placeholders[1].text = (
    "• 智能NPC：行为树/深度学习赋予NPC智能决策，个性化互动，提升沉浸感。\n• 自然语言处理：NLP让NPC与玩家自然对话，智能对话、剧情生成。\n• 计算机视觉：图像识别、动作捕捉提升真实感，安全检测外挂。\n• 内容生成与推荐：AI自动生成地图、任务、剧情，个性化推荐，提升创新效率。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/5.png', "AI NPC实际案例（数据来源：腾讯GiiNEX/逆水寒）", 5)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：每个技术点依次淡入，图片淡入"

# 6. AI在现代游戏开发中的应用
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "三、AI在现代游戏开发中的应用"
slide.placeholders[1].text = (
    "• 智能NPC：学习和适应能力，多人在线生态管理。\n• 关卡生成：动态生成关卡、剧情、任务，体验独特。\n• 数据分析与测试：AI分析玩家数据，自动化测试提升效率。\n• 推荐系统与个性化：动态调整难度和内容，提升玩家满意度。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/6.png', "AI自动生成关卡流程（数据来源：Vox/EA/Ubisoft）", 6)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：每个应用点依次淡入，图片淡入"

# 7. 反外挂与安全
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "四、AI在现代游戏开发中的应用-反外挂与安全"
slide.placeholders[1].text = (
    "• AI自动抓取玩家行为特征值，识别异常操作。\n• 算法流程：数据采集→特征提取→模型训练→实时检测→预警/处理。\n• 案例：腾讯安全盾、网易天眼、暴雪守望先锋。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/7.png', "AI反外挂系统流程图（数据来源：ITHome 2022）", 7)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：流程分步淡入，图片淡入"

# 8. 行业数据与趋势
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "五、行业数据与趋势"
slide.placeholders[1].text = (
    "• 2025年全球AI游戏市场26亿美元，年复合增长率28.8%。\n• 亚太地区最大，移动平台占比高。\n• AI市场增长远超传统游戏市场。\n• 主要企业：微软、腾讯、索尼、NVIDIA、Unity、Epic Games。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/8.png', "全球AI游戏市场份额（数据来源：Dimension Market Research 2025）", 8)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：分点依次淡入，图表淡入"

# 9. 未来展望与挑战
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "六、未来展望与挑战"
slide.placeholders[1].text = (
    "• 算法复杂、算力需求高。\n• 数据隐私与安全问题。\n• AI偏见与伦理挑战。\n• 未来：虚拟世界、智能内容生产、跨平台协作。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/9.png', "AI+元宇宙未来趋势（数据来源：百度图片/Meta/Unity）", 9)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：分点依次淡入，图片淡入"

# 10. 个人分析与见解
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "七、个人分析与见解"
slide.placeholders[1].text = (
    "• AI将彻底改变游戏开发和体验方式。\n• 推动游戏智能化、个性化、创新。\n• 开发者需不断学习新技术。\n• 未来游戏更智能、有趣、富有创造力。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/10.png', "AI赋能个人成长（数据来源：Convai Blog 2024）", 10)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：分点依次淡入，图片淡入"

# 11. 结论
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "八、结论"
slide.placeholders[1].text = (
    "• AI深刻改变游戏行业各环节。\n• 创新潜力巨大，未来可期。\n• 需关注AI伦理与安全，共同推动行业进步。"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/11.png', "AI推动行业变革（数据来源：Dimension Market Research 2025）", 11)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：分点依次淡入，图片淡入"

# 12. 参考文献/致谢
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "九、参考文献/致谢"
slide.placeholders[1].text = (
    "1. Dimension Market Research 2024\n2. Newzoo 2023\n3. Allied Market Research 2024\n感谢老师和同学们的支持！"
)
layout_type, text_left, text_top, text_box_width, text_box_height = add_img_with_caption_random(slide, 'ppt_imgs/12.png', "权威报告引用页（数据来源：Dimension Market Research 2025）", 12)
slide.shapes.add_textbox(text_left, text_top, text_box_width, text_box_height).text_frame.text = "动画建议：参考文献、致谢依次淡入，图片淡入"

prs.save('AI_游戏开发PPT_动画建议版.pptx')
print('已生成动画建议版PPT：AI_游戏开发PPT_动画建议版.pptx') 